package com.zut.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.common.R;
import com.zut.dto.DishDto;
import com.zut.entity.Category;
import com.zut.entity.Dish;
import com.zut.entity.DishFlavor;
import com.zut.service.CategoryService;
import com.zut.service.DishFlavorService;
import com.zut.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;


    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Dish::getName,name);
        queryWrapper.eq(Dish::getIsDeleted,0);
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,queryWrapper);
        Page<DishDto> dtoPageInfo = new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPageInfo,"records");
       List<Dish> records= pageInfo.getRecords();
       List<DishDto> collect =records.stream().map(item->{
           DishDto dishDto = new DishDto();
           BeanUtils.copyProperties(item,dishDto);
           Long categoryId = item.getCategoryId();
           Category category = categoryService.getById(categoryId);
           String categoryName = category.getName();
           dishDto.setCategoryName(categoryName);
           return dishDto;
       }).collect(Collectors.toList());
            dtoPageInfo.setRecords(collect);
        return R.success(dtoPageInfo);
    }

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
       log.info("dishDto:{}",dishDto);

       dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }


    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id) {
          DishDto dishDto =dishService.getByIdWithFlavor(id);

         return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        dishService.deleteWithFlavor(ids);

        return R.success("删除菜品成功");
    }

    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable Integer status,String ids){
        dishService.changeStatus(status,ids);

        return R.success("修改状态成功");
    }

   /* @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){

        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(dish.getName()),Dish::getName,dish.getName())
                .eq(!Objects.isNull(dish.getCategoryId()),Dish::getCategoryId,dish.getCategoryId())
                .eq(Dish::getStatus,1)
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime);
        List<Dish> list=dishService.list(queryWrapper);
        return R.success(list);
    }*/

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){

        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(dish.getName()),Dish::getName,dish.getName())
                .eq(!Objects.isNull(dish.getCategoryId()),Dish::getCategoryId,dish.getCategoryId())
                .eq(Dish::getStatus,1)
                .orderByAsc(Dish::getSort)
                .orderByDesc(Dish::getUpdateTime);
        List<Dish> list=dishService.list(queryWrapper);

        List<DishDto> collect=list.stream().map(item->{
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long dishId=item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper();
             lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
             List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);

             dishDto.setFlavors(dishFlavorList);

             return dishDto;
        }).collect(Collectors.toList());
        return R.success(collect);
    }
}
